@extends('mobile.layout1')
@section('title','订单')
@section('aside-bar')

@endsection
@section('content')


    <body>
    <div class="view-container view-order">
        <div class="view-inner">
            <!-- 订单信息 -->
            <section class="order-head-bar">
                <i class="if if-order"></i>
                <p>您于 {{$order->create_time}} 下的订单<br>请在 <span
                            class="over-orderdue-min">{{config('app.order_ttl')}}</span>分内付款，超时订单将自动取消。</p>
            </section>
            <!-- 配送 -->
            <section class="order-express">
                {{--<div class="switch-logis">--}}
                {{--<button type="button" class="btn-sm change-express-btn btn-success-o" data-type="0">快递配送</button>--}}
                {{--</div>--}}
                <div class="selected-logis">
                    <!-- 没有默认地址显示    -->

                    <input type="hidden" id="order_id" value="{{$order->order_pay_id}}">
                    <!-- 有默认地址时显示 -->
                    @if($order->address_id)
                        <a href="/my/address?redirect_uri={{urlencode(Request::fullUrl())}}"
                           class="address confirm-change-address-btn">
                            <div class="address-info clearfix">
                                <span class="address-name">收货人: {{$order->address->address_object['consignee']}}</span>
                                <span class="address-phone">{{$order->address->address_object['mobile']}}</span>
                            </div>
                            <div class="address-detail"> {{$order->address->address_object['province']}} {{$order->address->address_object['city']}} {{$order->address->address_object['district']}}{{$order->address->address_object['address']}}</div>
                        </a>
                        <input type="hidden" name="address_id" id="address_id" value="{{$order->address->address_id}}">
                    @elseif($address)

                        <a href="/my/address?redirect_uri={{urlencode(Request::fullUrl())}}"
                           class="address confirm-change-address-btn">
                            <div class="address-info clearfix">
                                <span class="address-name">收货人: {{$address->consignee}}</span>
                                <span class="address-phone">{{$address->mobile}}</span>
                            </div>
                            <div class="address-detail"> {{$address->province}} {{$address->city}} {{$address->district}}{{$address->address}}</div>
                        </a>
                        <input type="hidden" name="address_id" id="address_id" value="{{$address->address_id}}">
                    @else
                        <a class="add-address confirm-add-address-btn"
                           href="/my/address/show?redirect_uri={{urlencode(Request::fullUrl())}}">新增收货地址</a>
                    @endif
                </div>
            </section>
            <!-- 商品 -->
            <section class="order-goods">
                <ul class="cart-list confirm-cart">
                    @if(isset($order->goods)&&count($order->goods))
                        @foreach($order->goods as $v)
                            <li>
                                <div class="item-info">
                                    <a href="{{route('goods.show',['id'=>$v->good_object['goods_id']])}}"
                                       class="item-image">
                                        <img src="{{$v->good_object['goods_thumbnail']}}">
                                    </a>
                                    <a href="{{route('goods.show',['id'=>$v->id])}}"
                                       class="item-title">{{$v->good_object['goods_name']}}</a> <span
                                            class="item-price">￥{{$v->good_price}}</span>
                                    <span class="quantity">× {{$v->good_object['buy_num']}}</span>
                                    <div class="item-sku-quantity">
                                        <span class="props"></span>
                                    </div>
                                </div>
                            </li>
                        @endforeach
                    @endif

                </ul>
                <div class="order-delivery">运费<span class="pull-right shipemnt-fee-num">免运费</span></div>
                {{--  <div class="order-message">
                      <textarea name="extra" id="extra" placeholder="给卖家留言..."></textarea>
                  </div>--}}
                <div class="goods-subtotal">
                    <span>共<em>{{$order->total_num}}</em>件商品</span> 合计:<strong
                            class="amount-sum-num">￥{{$order->total_money}}</strong>
                </div>
            </section>

        <!-- 优惠&折扣 -->
            <section class="order-ump">
                <div class="ump-item clearfix has-select  ">
                    <div class="title">优惠券</div>
                    <select name="user_coupon_id" id="user_coupon_id"
                            style="height: 2.2rem;margin:0.5rem 0 0.5rem 10px;">
                        <option value="">选择优惠券</option>
                        @if(isset($order->coupons))
                            <option value="{{$order->coupons->coupon_object['id']}}" data-price="{{$order->coupons->coupon_object['money']}}" selected>{{$order->coupons->coupon_object['name']}}</option>
                        @endif
                        @if($myCoupon->isNotEmpty())
                            @foreach($myCoupon as $v)
                                <option value="{{$v->id}}" data-price="{{$v->money}}" >{{$v->name}}</option>
                            @endforeach

                        @endif
                    </select>
                </div>
                <!--分享抵用券-->
            </section>
            @if($order->pay_balance!=0)
                <section class="order-total-num">
                    <span>余额扣除</span>
                    <div class="order-price amount-num" id="balance_money">￥ -{{$order->pay_balance}}</div>
                </section>
        @endif
        <!-- 应付 -->
            <section class="order-total-num">
                <span>实付</span>
                <div class="order-price amount-num" id="pay_price">￥<span id="total_money">{{$order->pay_price-$order->pay_balance}}</span></div>
            </section>


            <!-- 支付 -->
            <section class="pay-btn pay order-ump" style="margin-bottom: 0;">
                <div class="ump-item clearfix js-switch-balance">
                    <div class="title">余额￥{{Auth::user()->money}}</div>
                    <div class="interpret">
                        <input class="switch" type="checkbox" id="balance" name="checkbox" checked value="1"
                               switch-type="useBalance">
                    </div>
                </div>
                {{--    <div class="ump-item clearfix js-switch-balance">
                        <div class="title">支付宝</div>
                        <div class="interpret">
                            <input class="switch" type="checkbox" name="pay-type" value="2" switch-type="useBalance" >
                        </div>
                    </div>--}}
                <div class="ump-item clearfix js-switch-balance">
                    <div class="title">微信</div>
                    <div class="interpret">
                        <input class="switch" type="checkbox" name="checkbox-switch" value="3" switch-type="useBalance">
                    </div>
                </div>
                <div>
                    <section class="order-pay">
                        <button type="button" class="btn btn-block payment-btn btn-yellow payment-llpay"
                                onclick="payfor()">确定支付
                        </button>
                    </section>
                </div>
            </section>
        </div>
    </div>
    <div class="popup popup-list-coupon remove-on-close modal-in">
        <div class="close-popup if if-close"></div>
        <h4 class="popup-title"
            style="line-height: 3.2rem;font-size: 1.3rem;padding: 0 5rem 0 .85rem;font-weight: inherit;">选择优惠券</h4>
        <section class="address-list js-coupon-list my-coupon"
                 style="max-height: 20em; overflow-y: scroll; -webkit-overflow-scrolling: touch; overflow-scrolling: touch;">
            <ul class="js-coupon-item">
                <li class="empty-info">
                    <p class="icon"><i class="if if-coupon"></i></p>
                    <p>还没可用的优惠券，试试优惠码吧！</p>
                </li>
                <!-- 优惠券列表 -->
                <li class="coupon-list"></li>
            </ul>
        </section>
        <ul class="list-block">
            <li class="input">
                <i class="if if-other"></i>
                <input type="text" name="consignee" class="form-item coupon-valid-input" placeholder="请输入优惠码"
                       style="padding-right: 4.2rem;">
                <button class="button btn-right coupon-valid-btn" style="color: #ff3608;border-color: #ff3608;">验证
                </button>
            </li>
        </ul>
    </div>
    <div class="popup-overlay"></div>

    </body>
@endsection
@section('js')

    <script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
        var user_coupon ={!! $myCoupon->toJson()!!};
        @if(isset($order->coupons))
            var total_money={{$order->total_money}};
                @else
            var total_money=$("#total_money").html();
            @endif

        //返回利润
        function find(id) {
            var lenght = user_coupon.length;
            for (var i = 0; i < lenght; i++) {
                if (id == user_coupon[i].id) {
                    return user_coupon[i].money;
                }
            }
        }

        $('#user_coupon_id').change(function () {
          var money= find($(this).val());
            console.log(money);

          //计算价格
            if(money>0){
                var price=total_money-money >0 ? total_money-money :0 ;
                $("#total_money").html(price);
            }else{
                $("#total_money").html(total_money);
            }

        });
        $('#balance').click(
            function () {
                var checked = $(this).val();
                if (checked) {

                }
            }
        );
        wx.config({!! $jsSdk !!});
        $(function () {
            $('.coupon-open-btn').click(function () {
                $('.modal-in').show();
                $('.popup-overlay').show();
            })

        })
        function payfor() {
            //订单id
            var index = layer.open({
                type: 2
                , content: '订单支付中'
            });
            var order_id = $("#order_id").val();
            var address_id = $("#address_id").val();
            var user_coupon_id = $("#user_coupon_id").val();//用户优惠券id
            /*  var extra=$("#extra").html();//用户id*/
            var pay_type = $("input[name='checkbox-switch']:checked").val();
            var is_banlance = $("input[name='checkbox']:checked").val() == 'undefind' ? 0 : $("input[name='checkbox']:checked").val();

            $.post('/order/payfor',
                {
                    order_id: order_id,
                    address_id: address_id,
                    user_coupon_id: user_coupon_id,

                    pay_type: pay_type,
                    is_banlance: is_banlance,

                }, function (data) {

                    if (data.code == '0') {
                        layer.open({
                            content: data.message,
                            time: 2
                            , skin: 'msg'
                        });
                        layer.close(index);
                        window.reload;
                    } else if (data.code == 1) {
                        if (pay_type == 1) {
                            layer.open({
                                content: data.message,
                                time: 2
                                , skin: 'msg'
                            });
                            layer.close(index);
                            location.href = '{{route('order.success',['order'=>$order->order_pay_id])}}';

                        } else if (pay_type == 3) {
                            if (data.code == 1) {
                                layer.open({
                                    content: data.message,
                                    time: 2
                                    , skin: 'msg'
                                });
                                layer.close(index);
                                location.href = '{{route('order.success',['order'=>$order->order_pay_id])}}';
                            }
                            ;


                        } else if (pay_type == 2) {//支付表支付
                            location.href = '/alipay?orderPayId={{$order->order_pay_id}}';

                        } else {
                            layer.open({
                                content: data.message,
                                time: 2
                                , skin: 'msg'
                            });
                            layer.closeAll();
                            location.href = '{{route('order.success',['order'=>$order->order_pay_id])}}';
                        }

                    } else if (data.code == 2) {
                        WeixinJSBridge.invoke(
                            'getBrandWCPayRequest', data.message,
                            function (res) {
                                if (res.err_msg == "get_brand_wcpay_request:ok") {
                                    layer.close(index);
                                    location.href = '{{route('order.success',['order'=>$order->order_pay_id])}}';
                                }
                                if (res.err_msg == "get_brand_wcpay_request:fail") {
                                    layer.close(index);
                                    location.reload(force)

                                }
                                if (res.err_msg == "get_brand_wcpay_request:cancel") {
                                    layer.close(index);
                                    location.reload(force)

                                }

                            }
                        );
                    }
                })
        }
    </script>

    @endsection
    </html>
